package com.oa.ylms.apps.dao.news.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.dao.news.INewsInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;
import com.oa.ylms.apps.entity.news.NewsInfoValue;

public class NewsInfoDaoImpl extends HibernateDaoSupport implements INewsInfoDao {
	private List<NewsInfoValue> list;
	private Integer numbers;
	
	// DAO method
	@Override
	public int addNewsInfo(NewsInfoValue newsInfo) throws Exception {
		if (newsInfo != null) {
			this.getHibernateTemplate().save(newsInfo);
			return newsInfo.getNewsId();
		}
		return 0;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<NewsInfoValue> qureyNewsWithPage(final NewsInfoValue newsInfo, final ArchiveInfoValue archiveInfo, final int rows,final int pageNo)
			throws Exception {
			String hqlStr = "SELECT niv FROM NewsInfoValue niv ,StaffInfoValue siv, ArchiveInfoValue aiv WHERE 1=1 AND siv.staffId=niv.staffInfo.staffId AND siv.archiveInfo.archiveId = aiv.archiveId";
			if (newsInfo.getNewsCategory() != null) {
				hqlStr += " AND niv.newsCategory = ?";
			}
			if (newsInfo.getPublishTime() != null){
				hqlStr += " AND niv.publishTime <= ?";
			}
			if (newsInfo.getTitle() != null){
				hqlStr += " AND niv.title like ?";
			}
			if(archiveInfo.getStaffName() != null){
				hqlStr += " AND aiv.staffName like ?";
			}
			hqlStr += " ORDER BY niv.publishTime DESC";
			final String hql = hqlStr;
			list = this.getHibernateTemplate().executeFind(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							Query query = session.createQuery(hql);
							int index = -1;
							if (newsInfo.getNewsCategory() != null) {
								query.setEntity(++index, newsInfo.getNewsCategory());
							}
							if (newsInfo.getPublishTime() != null){
								query.setDate(++index, newsInfo.getPublishTime());
							}
							if (newsInfo.getTitle() != null){
								query.setString(++index, newsInfo.getTitle());
							}
							if (archiveInfo.getStaffName() != null){
								query.setString(++index, archiveInfo.getStaffName());
							}
							query.setFirstResult(rows * (pageNo - 1));
							query.setMaxResults(rows);
							list = query.list();
							return list;
						}
					});
			return list;
	}
	@Override
	public NewsInfoValue qureyNewsById(NewsInfoValue newsInfo) throws Exception {
		return (NewsInfoValue) this.getHibernateTemplate().get(NewsInfoValue.class, newsInfo.getNewsId());
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<NewsInfoValue> qureyTopNews(int topNum)
			throws Exception {
		String hql = "SELECT niv TOP "+topNum+ " FROM NewsInfoValue niv ORDER BY niv.publishTime DESC";
		return this.getHibernateTemplate().find(hql,topNum);
	}
	
	@Override
	public boolean modifyNewsInfo(NewsInfoValue newsInfo) throws Exception {
		if (newsInfo != null) {
			try {
				this.getHibernateTemplate().update(newsInfo);
			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}
	
	@Override
	public boolean deleteNewsInfo(NewsInfoValue newsInfo) throws Exception {
		if (newsInfo != null) {
			try {
				this.getHibernateTemplate().delete(newsInfo);
			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countTPages(final NewsInfoValue newsInfo, final ArchiveInfoValue archiveInfo, int rows) throws Exception {
			int pageNumbers = 0;
			String hqlStr = "SELECT niv FROM NewsInfoValue niv ,StaffInfoValue siv, ArchiveInfoValue aiv WHERE 1=1 AND siv.staffId=niv.staffInfo.staffId AND siv.archiveInfo.archiveId = aiv.archiveId";
			if (newsInfo.getNewsCategory() != null) {
				hqlStr += " AND niv.newsCategory = ?";
			}
			if (newsInfo.getPublishTime() != null){
				hqlStr += " AND niv.publishTime <= ?";
			}
			if (newsInfo.getTitle() != null){
				hqlStr += " AND niv.title like ?";
			}
			if(archiveInfo.getStaffName() != null){
				hqlStr += " AND aiv.staffName like ?";
			}
			hqlStr += " ORDER BY niv.publishTime DESC";
			final String hql = hqlStr;
			list = this.getHibernateTemplate().executeFind(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							Query query = session.createQuery(hql);
							int index = -1;
							if (newsInfo.getNewsCategory() != null) {
								query.setEntity(++index, newsInfo.getNewsCategory());
							}
							if (newsInfo.getPublishTime() != null){
								query.setDate(++index, newsInfo.getPublishTime());
							}
							if (newsInfo.getTitle() != null){
								query.setString(++index, newsInfo.getTitle());
							}
							if (archiveInfo.getStaffName() != null){
								query.setString(++index, archiveInfo.getStaffName());
							}
							list = query.list();
							return list;
						}
					});
			numbers = list.size();
			if (numbers <= rows) {
				pageNumbers = 1;
			} else {
				if (numbers % rows == 0) {
					pageNumbers = numbers / rows;
				} else {
					pageNumbers = numbers / rows + 1;
				}
			}
			return pageNumbers;
	}
	@Override
	public int getTotalNumbers() throws Exception{
		return getNumbers();
	}
	
	// DAO property accessors
	
	public List<NewsInfoValue> getList() {
		return list;
	}

	public void setList(List<NewsInfoValue> list) {
		this.list = list;
	}
	
	public void setNumbers(Integer numbers) {
		this.numbers = numbers;
	}

	public Integer getNumbers() {
		return numbers;
	}

}
